package conson.tech.manage.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import conson.tech.manage.app.entity.pojo.AppAdvertPlace;
import conson.tech.manage.app.entity.vo.AppAdvertPlaceVo;
import conson.tech.manage.app.mapper.AppAdvertMapper;
import conson.tech.manage.app.mapper.AppAdvertPlaceMapper;
import conson.tech.manage.app.service.AppAdvertPlaceService;
import conson.tech.manage.common.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: AppAdvertPlaceServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/4/23 8:25 上午
 * Version: 1.0
 */
@Service("appAdvertPlaceService")
public class AppAdvertPlaceServiceImpl implements AppAdvertPlaceService {
    @Autowired
    private AppAdvertPlaceMapper appAdvertPlaceMapper;
    @Autowired
    private AppAdvertMapper appAdvertMapper;
    @Transactional
    @Override
    public int createAdvertPlace(AppAdvertPlaceVo vo) {
        AppAdvertPlace appAdvertPlace = new AppAdvertPlace();
        BeanUtils.copyProperties(vo, appAdvertPlace);
        //appAdvertPlace.setCreatedBy();
        appAdvertPlace.setDelFlag(1);
        appAdvertPlace.setCreatedTime(LocalDateTime.now());
        appAdvertPlace.setUpdatedTime(LocalDateTime.now());
        return appAdvertPlaceMapper.insert(appAdvertPlace);
    }
    @Transactional
    @Override
    public int updateAdvertPlace(AppAdvertPlaceVo vo) {
        AppAdvertPlace appAdvertPlace = new AppAdvertPlace();
        BeanUtils.copyProperties(vo, appAdvertPlace);
        appAdvertPlace.setUpdatedTime(LocalDateTime.now());
        return appAdvertPlaceMapper.updateById(appAdvertPlace);
    }

    @Override
    public List<AppAdvertPlaceVo> queryAdvertPlace() {
        QueryWrapper query = new QueryWrapper();
        AppAdvertPlace place = new AppAdvertPlace();
        place.setDelFlag(1);
        query.setEntity(place);
        query.orderByAsc("id");
        List<AppAdvertPlace> appAdvertPlaces = appAdvertPlaceMapper.selectList(query);

        List<AppAdvertPlaceVo> list = EntityUtils.listToList(appAdvertPlaces, AppAdvertPlaceVo.class);
        if (!CollectionUtils.isEmpty(list)) {
            for (AppAdvertPlaceVo vo : list) {
                Integer num = appAdvertMapper.selectAdvertNumByPlaceId(vo.getId());
                vo.setNum(num==null?0:num);
            }
        }
        return list;
    }
}
